package slidingwindow;

public class MaximizeTheConfusionOfAnExam2024 {
    public int maxConsecutiveAnswers(String answerKey, int k) {
        return Math.max(slideWindow(answerKey, k, 'T'), slideWindow(answerKey, k, 'F'));
    }

    private int slideWindow(String answerKey, int k, Character c) {
        int slow = 0;
        int quick = 0;
        int count = 0;
        int result = 0;
        while (quick < answerKey.length()) {
            char cur = answerKey.charAt(quick);
            if (cur == c) {
                count++;
            }
            if (count > k) {
                while (count >k && slow<answerKey.length()){
                    if(answerKey.charAt(slow)==c){
                        count--;
                    }
                    slow++;
                }
            }
            result = Math.max(result,quick-slow+1);

            quick++;

        }
        return result;

    }

}
